package com.barunlab.ems.server.sms.user;

import java.util.ArrayList;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;

import com.barunlab.ems.db.SqlSessionFactoryManager;
import com.barunlab.ems.model.UserInfo;

public class UserDao {
	private static Logger LOGGER = Logger.getLogger(UserDao.class);

	private SqlSessionFactory sqlMapper = SqlSessionFactoryManager.getSqlSessionFactory();

	@SuppressWarnings({"rawtypes", "unchecked"})
	public ArrayList<UserInfo> list(UserInfo entity) {
		LOGGER.debug("list");
		
		SqlSession session = sqlMapper.openSession();
		
		ArrayList resultList = null;
		try {
			resultList = (ArrayList)session.selectList("sms.user.list", entity);
		}
		finally {
			if ( session != null ) {
				session.close();
			}
		}
		
		return resultList; 
	}
	
	public int insert(UserInfo userInfo) {
		LOGGER.debug("insert");
		
		SqlSession session = sqlMapper.openSession();
		
		int result = 0;
		try {
			result = session.insert("sms.user.insert", userInfo);
			session.commit();
		}
		catch (Exception ex) {
			LOGGER.error(ex.getMessage(), ex);
			session.rollback();
		}
		finally {
			if ( session != null ) {
				session.close();
			}
		}
		
		return result;
	}
}
